package com.xintonghua.bussiness.ui.user;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.MyUtils;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.base.widget.ConfirmDialog;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.GroupLogAdapter;
import com.xintonghua.bussiness.adapter.StockManageAdapter;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.GoodsListBean;
import com.xintonghua.bussiness.bean.GroupLogBean;
import com.xintonghua.bussiness.bean.GroupLogResponse;
import com.xintonghua.bussiness.bean.StockBean;
import com.xintonghua.bussiness.bean.StockOrderBean;
import com.xintonghua.bussiness.bean.StockOrderResponse;
import com.xintonghua.bussiness.event.StockEventBus;
import com.xintonghua.bussiness.ui.user.manage.SelectSaltActivity;
import com.xintonghua.bussiness.util.GlideCacheUtil;
import com.xintonghua.bussiness.util.MyState;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：wanxianyi
 * 时间：2018/3/5.
 * 包名：com.xintonghua.bussiness.ui.user
 */

public class StockOrderManageActivity extends BaseActivity {
    @BindView(R.id.tab_stock)
    TabLayout tabStock;
    @BindView(R.id.lv_stock)
    XRecyclerView lvStock;
    @BindView(R.id.tv_order_number)
    TextView tv_order_number;
    @BindView(R.id.tv_order_money)
    TextView tv_order_money;
    StockManageAdapter adapter;
    private String[] title = new String[]{"待收货", "已收货"};
    //1待收货  2已收货
    private int status = 1;
    private int page = 1;
    StockOrderResponse response;
    private List<StockOrderBean> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stock_order_manage);
        ButterKnife.bind(this);
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        setSimpleActionBar("进货订单管理");
        EventBus.getDefault().register(this);
        for (String str : title) {
            tabStock.addTab(tabStock.newTab().setText(str));
        }
        RefreshUtils.initList(this, lvStock, MyUtils.dip2px(this, 8), R.color.grey_bg);
        adapter = new StockManageAdapter(this, list);
        lvStock.setAdapter(adapter);
        lvStock.setLoadingListener(this);
        find();
        tabStock.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getPosition() == 0) {
                    status = 1;
                } else {
                    status = 2;
                }
                find();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        httpCent.getThisMonth(this, MyState.requestFour);

    }

    private void find() {
        httpCent.shopList(status, page, MyState.SIZE, this, MyState.requestOne);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                if (page == 1) {
                    list.clear();
                }
                response = JSONObject.parseObject((String) data, StockOrderResponse.class);
                list.addAll(response.getList());
                adapter.notifyDataSetChanged();
                RefreshUtils.onRefresh(adapter, lvStock);
                break;
            case MyState.requestTwo:
                find();
                break;
            case MyState.requestThree:
                Bundle bundle = new Bundle();
                bundle.putString(BuildConfig.FLAVOR, (String) data);
                bundle.putParcelable("data", stockOrderBean);
                openActivity(SelectSaltActivity.class, bundle);
                break;
            case MyState.requestFour:
                StockBean bean = JSONObject.parseObject((String) data, StockBean.class);
                tv_order_number.setText("" + bean.getCount());
                tv_order_money.setText("" + bean.getTotal());
                break;
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        find();

    }

    @Override
    public void onLoadMore() {
        if (RefreshUtils.hasNext(response, adapter, lvStock)) {
            page++;
            find();
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(final StockOrderBean bean) {
        if (bean.getStatus() == 1) {
            ConfirmDialog.showDialog(this, "提示", "是否确定签收？", "取消", "确定", null, new ConfirmDialog.OnRightListener() {
                @Override
                public void onClick(ConfirmDialog dialog) {
                    httpCent.sureGet(bean.getId(), StockOrderManageActivity.this, MyState.requestTwo);
                }
            });

        }

    }

    StockOrderBean stockOrderBean;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(StockEventBus bean) {
        stockOrderBean = bean.getBean();
        httpCent.getNumber(bean.getBean().getId(), this, MyState.requestThree);
    }
}
